package com.dbs.zstp.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = "com.dbs.zstp.epzh.mapper", sqlSessionTemplateRef="epzhSqlSessionTemplate")
public class EpzhlDataSourceConfig {

	private Logger logger = LoggerFactory.getLogger(this.getClass());


//  配置mapper的扫描，找到所有的mapper.xml映射文件  
    @Value("${epzh.mybatis.mapperLocations}")
    private String mapperLocations;  
    
//	 配置类型别名  
    @Value("${epzh.mybatis.typeAliasesPackage}")
    private String typeAliasesPackage;  

//  加载全局的配置文件  
    @Value("${epzh.mybatis.configLocation}")
    private String configLocation; 
    
	
	@Bean(name="epzhDataSource")
    @ConfigurationProperties(prefix="spring.datasource.epzh")
    @Primary //默认数据源、主数据源
	public DataSource dataSource() {
        logger.info("---------------------------epzh数据源初始化成功!--------------------------------");
		return DataSourceBuilder.create().type(com.alibaba.druid.pool.DruidDataSource.class).build();
	}

	/**
     *  提供SqlSessionFactory  
     * @param epzhDataSource
     * @return
     * @throws Exception
     */
    @Bean(name = "epzhSqlSessionFactory")
    @Primary  
    public SqlSessionFactory epzhSqlSessionFactory(@Qualifier("epzhDataSource")DataSource epzhDataSource) throws Exception{
            SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();  
            sessionFactoryBean.setDataSource(epzhDataSource);
            sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperLocations));  
            sessionFactoryBean.setTypeAliasesPackage(typeAliasesPackage);
            //设置mybatis-config.xml配置文件位置  
            sessionFactoryBean.setConfigLocation(new DefaultResourceLoader().getResource(configLocation));  

            return sessionFactoryBean.getObject();  
    }  
    
    /**
     * 配置事务
     * @param epzhDataSource
     * @return
     */
    @Bean(name="epzhDataSourceTransactionManager")
    @Primary
    public DataSourceTransactionManager epzhDataSourceTransactionManager(@Qualifier("epzhDataSource")DataSource epzhDataSource) {
    	return new DataSourceTransactionManager(epzhDataSource);
    }
    
    /**
     * 配置sqlSession
     * @param epzhSqlSessionFactory
     * @return
     * @throws Exception
     */
    @Bean(name="epzhSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate epzhSqlSessionTemplate(@Qualifier("epzhSqlSessionFactory")SqlSessionFactory epzhSqlSessionFactory)throws Exception {
    	return new SqlSessionTemplate(epzhSqlSessionFactory);
    }
    
    
    
    /**
     * 创建mysql
     * jdbc连接
     * @param epzhDataSource
     * @return
     */
    @Bean(name="epzhJdbcTemplate")
    public JdbcTemplate epzhJdbcTemplate(@Qualifier("epzhDataSource") DataSource epzhDataSource) {
    	return new JdbcTemplate(epzhDataSource);
    }
    
   /**
    * 将要执行的sql进行日志打印(不想拦截，就把这方法注释掉)  
    * @return
    */
//    @Bean
//    public SqlPrintInterceptor sqlPrintInterceptor(){  
//        return new SqlPrintInterceptor();  
//    }  

}